﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class TailingpondDailyWaterReposity : Reposity
    {
        public tailingpond_daily_water_info GetDailyWaterInfo(int tailingpondId, DateTime date)
        {
            var dailywater = from s in _dataContext.tailingpond_daily_water_infos
                             where tailingpondId == s.tailingpond_id && s.date == date.Date
                             select s;

            if (dailywater != null && dailywater.Count() > 0)
            {
                return dailywater.First();
            }
            else
            {
                return null;
            }
        }

        public IDictionary<DateTime, tailingpond_daily_water_info> QueryWaterInfo(int tailingpondId, DateTime dateFrom, DateTime dateTo)
        {
            var dailywater = from s in _dataContext.tailingpond_daily_water_infos
                             where tailingpondId == s.tailingpond_id && s.date >= dateFrom && s.date < dateTo
                             select s;

            IDictionary<DateTime, tailingpond_daily_water_info> infos = new Dictionary<DateTime, tailingpond_daily_water_info>();
            foreach (tailingpond_daily_water_info info in dailywater)
            {
                infos.Add(info.date, info);
            }

            return infos;
        }

        public tailingpond_daily_water_info CreateDailyWaterInfo(tailingpond_daily_water_info dailywater)
        {
            _dataContext.tailingpond_daily_water_infos.InsertOnSubmit(dailywater);
            SubmitChanges();
            return dailywater;
        }

        public bool DeleteDailyWaterInfo(tailingpond_daily_water_info dailywater)
        {
            _dataContext.tailingpond_daily_water_infos.DeleteOnSubmit(dailywater);
            return true;
        }
    }
}